﻿using System.Data.Common;
using System.Data;
using NHibernate.Driver;

namespace Inovout.Gis.Data
{
    public class GisConnection : DbConnection
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(GisConnection));

        private DbConnection rmdbConnection;
        private DriverBase rmdbDrive;
        public GisConnection(DriverBase rmdbDrive)
        {
            this.rmdbDrive = rmdbDrive;
            this.rmdbConnection = (DbConnection)rmdbDrive.CreateConnection();
        }
        protected override System.Data.Common.DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
        {
            return new GisTransaction(this, (DbTransaction)rmdbConnection.BeginTransaction(isolationLevel));
        }

        public override void ChangeDatabase(string databaseName)
        {
            rmdbConnection.ChangeDatabase(databaseName);
        }

        public override void Close()
        {
            rmdbConnection.Close();
        }

        public override string ConnectionString
        {
            get { return rmdbConnection.ConnectionString; }
            set { rmdbConnection.ConnectionString = value; }
        }

        protected override System.Data.Common.DbCommand CreateDbCommand()
        {
            return new GisCommand(rmdbDrive);
        }

        public override string DataSource
        {
            get { return rmdbConnection.DataSource; }
        }

        public override string Database
        {
            get { return rmdbConnection.Database; }
        }

        public override void Open()
        {
            rmdbConnection.Open();
        }

        public override string ServerVersion
        {
            get { return "4.0"; }
        }

        public override System.Data.ConnectionState State
        {
            get { return rmdbConnection.State; }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                rmdbConnection.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}
